package com.open.capacity.reimburse.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;
import com.open.capacity.commons.PageResult;
import com.open.capacity.model.system.SysUser;
import com.open.capacity.reimburse.dao.StdMettingDao;
import com.open.capacity.reimburse.entity.BillTraining2;
import com.open.capacity.reimburse.entity.BillTraining2Cost;
import com.open.capacity.reimburse.entity.StdMetting;
import com.open.capacity.reimburse.entity.StdTraining;
import com.open.capacity.reimburse.service.StdMettingService;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.DateUtil;
import com.open.capacity.utils.SysUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class StdMettingServiceImpl implements StdMettingService {

    @Autowired
    private StdMettingDao stdmettingDao;

    /**
     * 添加
     *
     * @param stdmetting
     */
    public int save(StdMetting stdmetting) throws Exception {
        SysUser loginUser = SysUserUtil.getLoginAppUser();

        validition(stdmetting);

        stdmetting.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        stdmetting.setUpdateUserid(loginUser.getId());
        stdmetting.setUpdateUsername(loginUser.getRealName());
        stdmetting.setUpdateTime(new Date());
        return stdmettingDao.save(stdmetting);
    }

    private void validition(StdMetting stdmetting) throws Exception {
        StdMetting exists = stdmettingDao.findByCategory(stdmetting.getCategory());
        if (exists != null && !exists.getId().equals(stdmetting.getId())) {
            throw new Exception("不允许同一个会议类别配置多次");
        }
    }

    /**
     * 修改
     *
     * @param stdmetting
     */
    public int update(StdMetting stdmetting) throws Exception {
        validition(stdmetting);

        stdmetting.setUpdateTime(new Date());
        return stdmettingDao.update(stdmetting);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return stdmettingDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public StdMetting findById(String id) {
        return stdmettingDao.findById(id);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<StdMetting> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<StdMetting> list = stdmettingDao.findAll(params);

        PageInfo<StdMetting> pageInfo = new PageInfo(list);

        return PageResult.<StdMetting>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }


    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<StdMetting> findAll(Map<String, Object> params) {
        return stdmettingDao.findAll(params);
    }


    @Override
    public String verify(String userId, Integer attendCount, Integer workerCount, Date startDate, Date endDate, BigDecimal money) {
        String error_msg = "";

        int days = DateUtil.getBetweenDay(startDate, endDate);
        if (days <= 0) {
            days = 1;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("category", 2);
        List<StdMetting> list = stdmettingDao.findAll(params);

        boolean flag;
        if (CollectionUtils.isNotEmpty(list)) {
            StdMetting item = list.get(0);
            BigDecimal standard = item.getTotal().multiply(BigDecimal.valueOf(days)).multiply(BigDecimal.valueOf(attendCount + workerCount));
            flag = money.compareTo(standard) <= 0;
            if (!flag) {
                DecimalFormat decimalFormat = new DecimalFormat("###,###.00");
                error_msg = "<p>⊙ 会议费用超标, 会议费用合计不得超过" + decimalFormat.format(standard) + "元</p>";
            }
        }
        return error_msg;
    }
}
